version 14.1
set more off
  quietly log
  local logon = r(status)
  if "`logon'" == "on" {
	log close 
	}
log using SPID_v1.2_static.log, text replace


/*	***************************************************************		*/
/* 	Author:			Scott LaCombe and Frederick J. Boehmke				*/
/*	Date:			June 21, 2021										*/
/*  File:			SPID_v1.2_static.do	  			      				*/
/*	Purpose:		Generate static rate scores from SPID v1.2 data.	*/
/*	Requires:		stpolinn.ado										*/
/*  Input File: 	SPID_v1.2.dta, 										*/
/*  				SPID_v1.2_policies.dta 								*/
/*  Output File: 	SPID_v1.2_static.dta								*/
/*	***************************************************************		*/

	/*********************************************************************/
	/* Uncomment and run these commands if you need to install stpolinn. */
	/*********************************************************************/


* net from http://myweb.uiowa.edu/fboehmke/stata/stpolinn
* net install stpolinn
	
	
	/*************************************************/
	/* Create the static innovation scores data set. */
	/*************************************************/

	/* read in SPID adoptions data and merge with policy information. */

use SPID_v1.2.dta, clear
merge m:1 policy using SPID_v1.2_policies.dta, nogenerate assert(match) keep(match)

	/* Replace Alaska and Hawaii with -9 before they become states since some */
	/* policies include them and others don't (even if not right-censored). */
	/* stpolinn will allow us to exclude them based on this value. */

replace adopt_year = -9 if inlist(state, "Alaska", "Hawaii") & first_year <= 1958

	/* Restrict to policies diffusing in 1912 or after. */

drop if first_year < 1912
	  
	/* Calculate static rate score. */
	/* Install stpolinn if needed. */

stpolinn adopt_year, score(rate) id(state) longform(policy) generate(rate_1912_2017) ///
	excludevalue(-9) saving(static1912_2017, replace)
	
	/* Now for 1959-2017 */

preserve

drop if first_year < 1959
	  
stpolinn adopt_year, score(rate) id(state) longform(policy) generate(rate_1959_2017) ///
	saving(static1959_2017, replace)

restore

	/* Now for 1912-1958, excluding AK and HI */

drop if first_year >= 1959

stpolinn adopt_year, score(rate) id(state) longform(policy) generate(rate_1912_1958) ///
	excludevalue(-9) replace

	/* Combine the data sets. */

merge 1:1 statenam using static1912_2017, nogenerat
merge 1:1 statenam using static1959_2017, nogenerat

	/* Label and Save */	

	label variable rate_1912_2017	"Static rate score, 1912-2017"
	label variable rate_1912_1958	"Static rate score, 1912-1958"
	label variable rate_1959_2017 	"Static rate score, 1959-2017"
	label variable statenam 		"State Name"

	compress

	label data "Static rate scores from SPID v1.2"

	saveold SPID_v1.2_static.dta, replace version(13)

	/* Clean up. */
	
erase static1912_2017.dta
erase static1959_2017.dta

clear
log close
exit, STATA

